package cn.cmkj.auction.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;


import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.cmkj.auction.R;
import cn.cmkj.auction.app.BaseActivity;
import cn.cmkj.auction.ui.fragment.MyOrderFragment;

@ContentView(R.layout.activity_order_list)
public class MyOrderListActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.title_back)
    private ImageButton titleBack;
    @ViewInject(R.id.title_text)
    private TextView titleText;
    @ViewInject(R.id.vp_view)
    private ViewPager mViewPager;
    @ViewInject(R.id.tab_layout)
    private TabLayout mTabLayout;

    private MyPagerAdapter adapter;
    private String[] titles = new String[] {"全部", "待付款", "待发货", "待收货"};
    private int currentItem = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        titleText.setText("我的订单");
        titleBack.setOnClickListener(this);
        currentItem = getIntent().getIntExtra("index", 0);
        adapter = new MyPagerAdapter(getSupportFragmentManager(), titles);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(currentItem);//判断是从哪跳进来的
    }


    //ViewPager适配器
    class MyPagerAdapter extends FragmentPagerAdapter {

        private String[] titles;

        public MyPagerAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return MyOrderFragment.create(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }
}
